<?php

	
    	
	$url = "https://api.twitter.com/1.1/followers/ids.json?";

	$oauth_access_token = '20862563-7A4HJ2Ta3pdAmseW5vZmoHXG6gXM5CAqH2aiKzANt';
	$oauth_access_token_secret = 'xHgGHLtllnD4CBqwRSMZWOjFv65Xn7mvllIp5Jp238';
	$consumer_key = 'BrTZa4y7WjufT1jkKPF2ww';
	$consumer_secret = 'sGrjhB2exsI0gSJB33e2gjcS2S8xHkTzbjvKNMNGhDs';


                
	require('tmhOAuth/tmhOAuth.php');
	require('tmhOAuth/tmhUtilities.php');
 	
 	$arrOAuth = array(
		"consumer_key" => $consumer_key, 
		"consumer_secret" => $consumer_secret, 
		"user_token" => $oauth_access_token, 
		"user_secret" => $oauth_access_token_secret,
		"curl_ssl_verifypeer" => false);
		
	$tmhOAuth = new tmhOAuth($arrOAuth);
 	
 	require_once('140dev_config.php');
	require_once('db_lib.php');
	$oDB = new db;
  
	if(!$oDB)
  	{
  		die('Could not connect: ' . mysql_error());
  	}
	
	if(isset($_GET['id_resto']))
		$result = $oDB->select("SELECT DS_SCREEN_NAME FROM MAST_TWITTER_ACCOUNTS T1 INNER JOIN MAST_RESTAURANTS T2 ON T1.DS_SCREEN_NAME = T2.DS_REST_TWITTER WHERE T2.ID_RESTAURANT = '" . $_GET['id_resto'] . "'");
	else
		$result = $oDB->select('SELECT DS_SCREEN_NAME FROM MAST_TWITTER_ACCOUNTS ORDER BY DT_LAST_FOLLOWERS_UPDATE ASC LIMIT 15');
	
	while($row = mysqli_fetch_row($result))
  	{
  		$arrParams = array('screen_name' => $row[0]);
  		
  		
  		$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/followers/ids.json'), $arrParams);
  		
		$response = $tmhOAuth->response['response'];
	
		$ids = json_decode($response, true);  
			
		$oDB->delete('REL_TWITTER_FOLLOWERS',"DS_SCREEN_NAME='" . $row[0] . "'");	
		foreach ( $ids as $id ) {
  			if ( is_array( $id ) ) {
    			foreach ( $id as $sub_result ) {
      				// You can store this value in a variable, or output it in your desired format.
      				//echo $sub_result . " sub_result <br />";
      				$oDB->insert('REL_TWITTER_FOLLOWERS',"DS_SCREEN_NAME='" . $row[0] . "', ID_USER_TWITTER='" . $sub_result  . "'");
    			}
  			}
		}	
	
		$oDB->update('MAST_TWITTER_ACCOUNTS', 'DT_LAST_FOLLOWERS_UPDATE=NOW()', "DS_SCREEN_NAME = '" . $row[0] . "'");
  	}	
?>